package com.ajie.bluetoothcard.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class Database {
	private DatabaseHelper mDbHelper = null;
	private Context mContext;
	private SQLiteDatabase mDb;

	public Database(Context context) {
		mContext = context;
		if (mDbHelper == null)
			mDbHelper = new DatabaseHelper(mContext);
		mDb = mDbHelper.getWritableDatabase();
	}

	public void close() {
		try {
			if (mDb.isOpen())
				mDb.close();
			mDbHelper = null;
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public Cursor query(String table, String column, String param, String orderby) {
		return mDb.query(table, null, " " + column + "=? ", new String[] { param }, null, null, orderby);
	}

	public Cursor query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy,
			String having, String orderBy, String limit) {
		return mDb.query(table, columns, selection, selectionArgs, groupBy, having, orderBy, limit);
	}

	public Cursor query(String table, String column, String param) {
		return mDb.query(table, null, " " + column + "=? ", new String[] { param }, null, null, null);
	}

	public Cursor query(String table) {
		return mDb.query(table, null, null, null, null, null, null);
	}

	public Cursor rawQuery(String sql, String[] args) {
		return mDb.rawQuery(sql, args);
	}

	public Cursor query(String table, String[] columns, String selection, String[] selectionArgs) {
		return mDb.query(table, columns, selection, selectionArgs, null, null, null);
	}

	public long insert(String table, ContentValues values) {
		long code = mDb.insert(table, null, values);
		return code;
	}

	public long update(String table, ContentValues values, String whereClause, String[] whereArgs) {
		long code = mDb.update(table, values, whereClause, whereArgs);
		return code;
	}

	public void delete(String table, String whereClause, String[] whereArgs) {
		mDb.delete(table, whereClause, whereArgs);
	}

	public void clearTable(String table) {
		mDb.delete(table, null, null);
	}
}
